<template>
  <div class="add">
    <el-dialog title="新增员工" :visible="dialogVisible" @close="closeEvent">
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="1-30个字符" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment" placeholder="请选择">
            <el-option
              v-for="item in employeesData.hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber" placeholder="1-20个字符" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div v-clickOut="clickOutEvent">
            <el-input
              v-model="form.departmentName"
              placeholder="请选择"
              readonly
              @focus="focusEvent"
            />
            <el-tree
              v-if="showTree"
              :data="treeData"
              :props="defaultProps"
              @node-click="handleNodeClick"
            />
          </div>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserPost } from '@/api/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      employeesData,
      showTree: false,
      treeData: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		姓名
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的手机号'))
              }
            }
          }
        ], //	string	非必须		手机号
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	number	非必须		聘用形式
        workNumber: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		工号
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	string	非必须		组织名称
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		入职时间
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须		转正时间
      }
    }
  },
  methods: {
    clickOutEvent() {
      this.showTree = false
    },
    // 组织架构点击处理
    handleNodeClick(data) {
      this.form.departmentName = data.name
      this.showTree = false
    },
    // 获取组织架构数据
    async focusEvent() {
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts, '')
      }
    },
    changeData(arr, pid = '') {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    closeEvent() {
      this.$emit('update:dialogVisible', false)
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('新增成功')
          this.closeEvent()
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.add {
}
</style>
